import { Controller, Get, Post, Body, BadRequestException, Req } from '@nestjs/common';
import { Request } from '@common/auth';
import { UserFocusService } from './userFocus.service';
import { FocusUserFocusDto } from './dto/focus-userFocus.dto';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { isMongoId } from 'class-validator';
import { ListUserFocusDto } from './dto/list-userFocus.dto';
@ApiTags('userFocus')
@ApiBearerAuth()
@Controller()
export class UserFocusController {
    constructor(private readonly userFocusService: UserFocusService) {}

    @ApiOperation({ summary: '用户关注' })
    @Post('/userFocus/focus')
    async focus(@Body() input: FocusUserFocusDto, @Req() req: Request) {
        if (!isMongoId(input.beFocusUser)) {
            throw new BadRequestException('id错误！');
        }
        if (input.beFocusUser === req.user._id) {
            throw new BadRequestException('自己不能关注自己');
        }
        try {
            return await this.userFocusService.focus(input, req.user);
        } catch (e) {
            throw new BadRequestException('请求失败', e?.message);
        }
    }
    @ApiOperation({ summary: '用户取消关注' })
    @Post('/userFocus/unfocus')
    async unfocus(@Body() input: FocusUserFocusDto, @Req() req: Request) {
        if (!isMongoId(input.beFocusUser)) {
            throw new BadRequestException('id错误！');
        }
        try {
            return await this.userFocusService.unfocus(input, req.user);
        } catch (e) {
            throw new BadRequestException('请求失败', e?.message);
        }
    }
    @ApiOperation({ summary: '获取当前用户关注/粉丝列表' })
    @Post('/userFocus/list')
    list(@Body() dto: ListUserFocusDto, @Req() req: Request) {
        if (!dto.where.beFocusUser && !dto.where.user) {
            throw new BadRequestException('缺少参数');
        }

        if (
            (dto.where.beFocusUser && !isMongoId(dto.where.beFocusUser)) ||
            (dto.where.user && !isMongoId(dto.where.user))
        ) {
            throw new BadRequestException('id错误！');
        }
        return this.userFocusService.list(dto, req.user);
    }
}
